@extends('admin.common.base-master')

@section('html-head')

@endsection

@section('html-body')
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <em></em>
            <a href="#">商品分类管理</a>
        </div>
        @include('admin.common.goods-header')
        <div class="filter-box clearfix">
            <div class="fr">
                <a class="addBtn" href="javascript:;" onclick="upPop(0,'','','',0,'')">新增</a>
            </div>
        </div>
        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>所属栏目</th>
                    <th>分类名称</th>
                    <th>分类图标</th>
                    <th>二级分类</th>
                    <th>分类排序</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @if(count($classifies) > 0)
                    @foreach($classifies as $key=>$classify)
                        <tr id="classify_{{$classify->id}}">
                            <td>{{$classify->id}}</td>
                            <td>{{$classify->section_name}}</td>
                            <td>
                                <span id="classify_name_{{$classify->id}}">{{$classify->name}}</span>
                            </td>
                            <td class="account-td" style="width: 5%;">
                                <span class="photo" style="border-radius: 100%;display:initial">
                                    @if($classify->img_id !== 0)
                                        <a target="_blank" href="{{\App\Http\Services\FileManager::getImgUrl($classify->img_id)}}"  class="openimg" data-key="{{$classify->img_id}}"><img src="{{\App\Http\Services\FileManager::getImgUrl($classify->img_id)}}" alt=""></a>
                                    @else
                                        <img src="{{url('images/default-avatar.png')}}" alt="">
                                    @endif
                                </span>
                            </td>
                            <td>
                                @if(count($classify->child) > 0)
                                    @foreach($classify->child as $menuChild)
                                        <p>{{$menuChild->name}}
                                            <a href="javascript:;" class="color2 del" style="margin-left: 5px" data-id="{{$menuChild->id}}">删除</a>
                                        </p>
                                    @endforeach
                                @endif
                            </td>
                            <td>{{$classify->sort}}</td>
                            <td>
                                <a class="color2" href="javascript:;" onclick="upPop(0,'{{$classify->id}}','',0)">新增子分类</a><span> | </span>
                                <a href="javascript:void(0);" data-id="{{$classify->id}}" style="color: #ef1414;" onclick="upPop('{{$classify->id}}',0,'{{$classify->name}}','{{$classify->sort}}','{{$classify->img_id}}','{{$classify->advert_img_url}}')">编辑</a> |
                                <a href="javascript:void(0);" class="del" data-id="{{$classify->id}}" >删除</a>
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
            </table>
        </div>
        <div class="pagelist" style="text-align: center;">
            <?php echo $classifies->render(); ?>
        </div>
    </div>

    <div class="upPop-box">
        <div class="backdrop"></div>
        <div id="upPop" class="info-pop tixian">
            <input type="hidden" name="id" id="classifyid" value=""/>
            <a class="close" href="javascript:void(0);"></a>
            <h3 id="set_title">新增子分类</h3>
            <div class="fill-list">
                <span class="fill-tit">分类名：</span>
                <input class="fill-ipt" type="text" name="name" id="name" value="">
            </div>

            <div class="fill-list">
                <span class="fill-tit">分类排序:</span>
                <input class="fill-ipt" type="text" name="sort" id="sort" value="">
            </div>

            <div class="fill-list">
                <span class="fill-tit">所属栏目:</span>
                <select id="classify">
                    <option value="0">请选择分类</option>
                    @foreach($sections as $section)
                        <option value="{{$section->id}}">{{$section->name}}</option>
                    @endforeach
                </select>
            </div>

            <div class="fill-list">
                <span class="fill-tit">图标:</span>
                <input class="fill-ipt" type="button" name="inputImage" id="inputImage" value="添加图片" />
                <input id="img_id" type="hidden" name="thumb_id" value="">
                <p class="text" id="uploadthumb">上传图片（图片格式为jpg、png、gif，规格：高限315px,文件小于2MB）</p>

{{--                <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">--}}
{{--                    <div class="btn" style="background: #00A2D4">--}}
{{--                        <span id="inputImage">添加图片</span>--}}
{{--                        <input id="img_id" type="hidden" name="thumb_id" value="">--}}
{{--                    </div>--}}
{{--                    <div class="progress">--}}
{{--                        <span class="bar"></span><span class="percent">0%</span >--}}
{{--                    </div>--}}
{{--                    <p class="text" id="uploadthumb">上传图片（图片格式为jpg、png、gif，规格：高限315px,文件小于2MB）</p>--}}
                    <div style="width: 100%;text-align: center"><img id="img_show" style="width: 100px;padding-top: 3px;display: none" src="" /></div>

{{--                </div>--}}
            </div>


            <input type="hidden" name="id" id="pid" />
            <div class="fill-list">
                <span class="fill-tit">&nbsp;</span>
                <input class="fill-submit" type="button" id="subThisForm" value="提交">
            </div>
        </div>
    </div>

    <script>
        function upPop(obj1,obj2,obj3,obj4,imgId=0,imgUrl=""){
            if(obj2 == 0){
                $("#set_title").html("新增父分类")
            }

            $('#img_id').val(imgId);
            if(imgUrl != ""){
                $("#img_show").show();
            }
            $('#img_show').val(imgUrl);
            $('#classifyid').val(obj1);
            $('#pid').val(obj2);
            $('#name').val(obj3);
            $('#sort').val(obj4);
            $('.upPop-box').upPop();

        }

        $(function(){
            $(document).on('click', '#subThisForm', function () {
                var name  = $('#name').val();
                var sort  = $('#sort').val();
                var pId   = $('#pid').val();
                var img_id= $('#img_id').val();
                var id    = $('#classifyid').val();
                var classifyId   = $('#classify option:selected').val();

                $.ajax({
                    url: '{{route("Api.Admin.sys.addClassify")}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name:name,
                        sort:sort,
                        pid:pId,
                        img_id:img_id,
                        type:'goods',
                        id:id,
                        classify_id:classifyId
                    },
                    success: function (obj) {
                        if (obj.level == "success") {
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose:function(){
                                        window.location.reload();
                                    }
                                }
                            });
                        } else if (obj.level == "error") {
                            noty({text: obj.message, type: obj.level, timeout: 600});
                        }

                    },
                    error: function (obj) {
                        console.log(obj);
                    }
                });
            });

            $(document).on("click",".del",function(){
                var id = $(this).attr("data-id");
                noty({
                    text: '可能影响分类内容，你确定要删除吗?',
                    buttons: [
                        {addClass: 'btn btn-primary', text: '确定', onClick: function($noty) {
                                $noty.close();
                                $.ajax({
                                    url:'{{route('Api.Admin.sys.delClassify')}}',
                                    type:'post',
                                    dataType:'json',
                                    data:{id:id},
                                    success:function(obj){
                                        if(obj.level == "success"){
                                            window.location.reload();
                                        }else{
                                            noty({type:obj.level,text:obj.message,timeout:600});
                                        }
                                    },
                                    error:function(obj){
                                        console.log(obj);
                                    }

                                });
                            }
                        },
                        {addClass: 'btn btn-danger', text: '取消', onClick: function($noty) {
                                $noty.close();
                            }
                        }
                    ]
                });

            });


            //缩略图上传代码
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage',
                url: '{{route("Api.File.upload")}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '5mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {},

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {
                        $('#uploadthumb').html('<span style="color:red">上传中...</span>');
                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        if (responseObj.level == "success") {
                            $('#img_id').val(responseObj.data.id);
                            var imgURL = responseObj.data.img_url + "?" + Math.random();
                            $('#img_show').attr("src",imgURL);
                            $('#uploadthumb').html('<span style="color:green">上传成功</span>');
                        } else {
                            noty({type: responseObj.level, text: responseObj.message, timeout: 800});
                            $('#uploadthumb').html('<span style="color: red">上传失败，请重新上传！</span>');
                        }

                    }
                }
            });
            uploader.init();
        });
    </script>
@endsection